package com.stylefeng.guns.modular.system.warpper;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.base.warpper.BaseControllerWarpper;
import com.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.modular.system.model.Gdhs;
import com.stylefeng.guns.modular.system.model.Gdprice;
import com.stylefeng.guns.modular.system.model.Gpdata;
import com.stylefeng.guns.modular.system.model.HyGpData;
import com.stylefeng.guns.modular.system.model.HybrankDetail;

public class HyBrankWarpper extends BaseControllerWarpper{

	public HyBrankWarpper(List<Map<String, Object>> list) {
        super(list);
    }

    @Override
    public void warpTheMap(Map<String, Object> map) {
    	DecimalFormat df = new DecimalFormat("0.00");
    	double jmMoney = ((Double)map.get("gJmMoney")) / 10000;
    	map.put("gJmMoney", jmMoney > 0 ? "<font color='red'><b>"+df.format(jmMoney)+"</b></font>" : "<font color='green'><b>"+df.format(jmMoney)+"</b></font>");
    	map.put("gTdate", ((Date)map.get("gTdate")).toString());
    	HybrankDetail hybrankDetail = new HybrankDetail();
    	hybrankDetail.setgSalesCode(((Long)map.get("gYybCode")).intValue());
    	try {
			hybrankDetail.setgTDate(DateUtils.parseDate(map.get("gTdate").toString(), "yyyyy-MM-dd"));
		} catch (ParseException e) {
		}
    	map.put("gSname", formatData(map.get("gSname").toString(),hybrankDetail));
    	
    	
    }
    
    private String formatData(String gSname,HybrankDetail hybrankDetail){
    	List<HyGpData> hyGpDatas = JSONObject.parseArray(gSname, HyGpData.class);
    	StringBuffer result = new StringBuffer();
    	for(HyGpData hyGpData : hyGpDatas){
    		DecimalFormat df = new DecimalFormat("0.00");
    		Gdhs gdhs =  ConstantFactory.me().getGdhs(StringUtils.substringBefore(hyGpData.getSCode(), "."));
    		Gdprice gdprice =  ConstantFactory.me().getGdprice(StringUtils.substringBefore(hyGpData.getSCode(), "."));
    		Gpdata gpdata = ConstantFactory.me().getGpdata(StringUtils.substringBefore(hyGpData.getSCode(), "."));
    		hybrankDetail.setgScode(StringUtils.substringBefore(hyGpData.getSCode(), "."));
    		hybrankDetail = ConstantFactory.me().getHybrankDetail(hybrankDetail);
    		if(gdhs == null || gdprice == null) continue;
    		result.append("</br>")
    		.append("<font color='red'><b>" + StringUtils.substringBefore(hyGpData.getSCode(), ".")+ "</b></font>")
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("<font color='red'><b>" + hyGpData.getCodeName() + "</b></font>")
    		.append("&nbsp&nbsp|&nbsp&nbsp股东增减比例:" + (gdhs.getgZjbl() > 0 ? "<font color='red'><b>"+gdhs.getgZjbl()+"</b></font>" : "<font color='green'><b>"+gdhs.getgZjbl()+"</b></font>"))
    		.append("</br>")
    		.append("最新价:" + (gdprice.getgPrice() > 0 ? "<font color='red'><b>"+gdprice.getgPrice()+"</b></font>" : "<font color='green'><b>"+gdprice.getgPrice()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("涨跌幅:" + (gdprice.getgZdf() > 0 ? "<font color='red'><b>"+gdprice.getgZdf()+"</b></font>" : "<font color='green'><b>"+gdprice.getgZdf()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("区间涨跌幅:" + (gdprice.getgJqZdf() > 0 ? "<font color='red'><b>"+gdprice.getgJqZdf()+"</b></font>" : "<font color='green'><b>"+gdprice.getgJqZdf()+"</b></font>"))
    		.append("</br>")
    		.append("净买入(万):" + (hybrankDetail.getgBmoney() > 0 ? "<font color='red'><b>"+df.format(hybrankDetail.getgBmoney()/10000)+"</b></font>" : "<font color='green'><b>"+df.format(hybrankDetail.getgBmoney()/10000)+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("净卖出(万):" + "<font color='green'><b>"+df.format(hybrankDetail.getgSmoney()/10000)+"</b></font>")
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("总买卖(万):" + (hybrankDetail.getgPbuy() > 0 ? "<font color='red'><b>"+df.format(hybrankDetail.getgPbuy()/10000)+"</b></font>" : "<font color='green'><b>"+df.format(hybrankDetail.getgPbuy()/10000)+"</b></font>"))
    		.append("</br>")
    		.append("私募(阳光):" + (gpdata.getgIsYgsmNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsYgsmNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsYgsmNum()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("私募(个人):" + (gpdata.getgIsGrsmNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsGrsmNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsGrsmNum()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("QFILL:" + (gpdata.getgIsQfillNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsQfillNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsQfillNum()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("基金社保:" + (gpdata.getgIsJjNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsJjNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsJjNum()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("国家队:" + (gpdata.getgIsGjdNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsGjdNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsGjdNum()+"</b></font>"))
    		.append("&nbsp&nbsp|&nbsp&nbsp")
    		.append("企业年金:" + (gpdata.getgIsQynjNum() > 0 ? "<font color='red'><b>"+gpdata.getgIsQynjNum()+"</b></font>" : "<font color='green'><b>"+gpdata.getgIsQynjNum()+"</b></font>"))
    		.append("</br></br>");
    	}
    	return result.toString();
    }
}
